package com.bsg.upm.check;

import java.text.MessageFormat;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Service;

import com.bsg.upm.constant.Messages;
import com.bsg.upm.entity.BusinessSystemEntity;
import com.bsg.upm.entity.UserEntity;

/**
 * 业务系统业务处理检查
 * 
 * @author HCK
 *
 */
@Service
public class BusinessSystemCheck extends BaseCheck {

	/**
	 * 新增时检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkSave(Map<String, Object> paramMap) {
		// 非逻辑检查
		CheckResult chkRS = checkSaveNonLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}

		// 逻辑检查
		chkRS = checkSaveLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 编辑时检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkUpdate(Map<String, Object> paramMap) {
		// 非逻辑检查
		CheckResult chkRS = checkUpdateNonLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}

		// 逻辑检查
		chkRS = checkUpdateLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 删除时检查
	 * 
	 * @param businessSystem
	 *            业务系统对象
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkRemove(BusinessSystemEntity businessSystem) {
		CheckResult chkRS = new CheckResult();

		// 存在性检查
		if (businessSystem == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "业务系统"));
			return chkRS;
		}

		// 关联性检查
		if (businessSubSystemDao.countByBusinessSystemId(businessSystem.getId()) > 0) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg("该业务系统已关联子系统，无法删除");
			return chkRS;
		}

		return chkRS;
	}

	/**
	 * 新增时非业务逻辑(非空和长度)检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkSaveNonLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		// 业务系统名称和长度检查
		String name = (String) paramMap.get("name");
		if (StringUtils.isBlank(name)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "业务系统名称"));
			return chkRS;
		}
		if (StringUtils.trim(name).length() > 64) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "业务系统名称", 64));
			return chkRS;
		}

		if (paramMap.containsKey("description")) {
			// 描述长度检查
			String description = (String) paramMap.get("description");
			if (StringUtils.trim(description).length() > 255) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "描述", 255));
				return chkRS;
			}
		}

		return chkRS;
	}

	/**
	 * 新增时业务逻辑检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkSaveLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();
		String name = (String) paramMap.get("name");
		UserEntity user = getUser();
		// 名称重复性检查
		if (businessSystemDao.countByNameAndOwner(name, user.getUsername()) > 0) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg(MessageFormat.format(Messages.EXIST, "业务系统名称"));
			return chkRS;
		}

		return chkRS;
	}

	/**
	 * 编辑时非业务逻辑(非空和长度)检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkUpdateNonLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();
		// 存在性检查
		String businessSystemId = (String) paramMap.get("businessSystemId");
		if (StringUtils.isBlank(businessSystemId)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "业务系统编码"));
			return chkRS;
		}

		if (paramMap.containsKey("name")) {
			// 业务系统名称和长度检查
			String name = (String) paramMap.get("name");
			if (StringUtils.isBlank(name)) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "业务系统名称"));
				return chkRS;
			}
			if (StringUtils.trim(name).length() > 64) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "业务系统名称", 64));
				return chkRS;
			}
		}

		if (paramMap.containsKey("description")) {
			// 描述长度检查
			String description = (String) paramMap.get("description");
			if (StringUtils.trim(description).length() > 255) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "描述", 255));
				return chkRS;
			}
		}

		return chkRS;
	}

	/**
	 * 编辑时业务逻辑检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkUpdateLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		// 存在性检查
		String businessSystemId = (String) paramMap.get("businessSystemId");
		BusinessSystemEntity businessSystem = businessSystemDao.get(businessSystemId);
		if (businessSystem == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "业务系统"));
			return chkRS;
		}

		String name = (String) paramMap.get("name");
		if (!name.equals(businessSystem.getName())) {
			// 名称重复性检查
			if (businessSystemDao.countByNameAndOwner(name, businessSystem.getOwner()) > 0) {
				chkRS.setStatus(HttpStatus.SC_CONFLICT);
				chkRS.setErrorMsg(MessageFormat.format(Messages.EXIST, "业务系统名称"));
				return chkRS;
			}
		}

		return chkRS;
	}
}
